@extends('layouts.layouts')
@section('title')
    <title>后台首页</title>
@endsection
@section('content')
    <link rel="stylesheet" href="/lib/layer/theme/default/layer.css"/>
    <script type="text/javascript" src="/lib/layer/layer.js"></script>
    <div class="content">
        <div class="header">
            <ul class="breadcrumb">
                <li><a href="/wechatSet/list">关键词</a></li>
                <li class="active">菜单列表</li>
            </ul>
        </div>
        <div class="main-content">
            <div class="btn-toolbar list-toolbar">

                <form class="form-inline" action="" method="get">

                    <a href="/wechatSet/keywordAdd"><button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal" ><i class="fa fa-plus"></i> 新增</button></a>

                    <!-- <div class="form-group">
                         <input type="text" class="form-control" name="title" placeholder="课程标题" value="">
                     </div>
                     <button type="submit" class="btn btn-default">筛选</button>-->
                </form>
            </div>
            <div class="row">
                <div class="col-sm-12 col-md-12">
                    <div class="panel panel-default">
                        <div class="panel-heading no-collapse">关键词列表</div>
                        <table class="table table-bordered table-striped">
                            <thead>
                            <tr>
                                <th>编号</th>
                                <th>关键词</th>
                                <th width="50%">回复内容</th>
                                <th>状态</th>
                                <th>操作</th>
                            </tr>
                            </thead>
                            <tbody>
                            @foreach($keywordList as $keyword)
                                <tr id="wechatSet_{{$keyword->id}}">
                                    <td>{{$keyword->id}}</td>
                                    <td>{{$keyword->keyword}}</td>
                                    <td>{{$keyword->reply_content}}</td>
                                   
                                    <td class="stateType_{{$keyword->id}}">{{$keyword->state==1?'有效':'失效'}}</td>
                                    <td>

                                        <a class="btn btn-success btn-xs" href="/wechatSet/keywordEdit?id={{$keyword->id}}">编辑</a>
                                        <button class="btn btn-danger btn-xs" data-id="{{$keyword->id}}" onclick="keywordSetDelete(this)">删除</button>
                                        <button class="btn btn-info btn-xs" data-id="{{$keyword->id}}" data-state="{{$keyword->state}}" onclick="keywordSetState(this)">{{$keyword->state?'取消使用':'使用'}}</button>
                                    </td>
                                </tr>
                            @endforeach
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
            {{$keywordList->render()}}
        </div>
    </div>
    <script type="text/javascript">
        function keywordSetDelete(obj){
            var id = $(obj).attr('data-id');
            var _token = '{{csrf_token()}}';
            var data = {id:id,_token:_token};
            layer.confirm('确定删除吗？', {
                btn: ['是', '否'],
            }, function(index){
                $.ajax({
                    url:'/wechatSet/delete',
                    data:data,
                    type:'POST',
                    dataType: 'json',
                    success:function(res){
                        layer.alert(res.message);
                        if(res.code == 0){
                            $("#wechatSet_"+id).remove();
                        }
                    }
                });
                layer.close(index);
                return true;
            }, function(index){
                layer.close(index);
            });
        }

        function keywordSetState(obj){
            var id = $(obj).attr('data-id');
            var state = $(obj).attr('data-state');
            if(state == 1){
                var notice = '取消使用吗？';
                var newState = 0;
                var name = '使用';
                var alias = '失效';
            }else{
                var notice = '使用吗？';
                var newState = 1;
                var name = '取消使用';
                var alias = '有效';
            }
            var _token = '{{csrf_token()}}';
            var data = {id:id,state:newState,_token:_token};
            var object = obj;
            layer.confirm(notice, {
                btn: ['是', '否'],
            }, function(index){
                $.ajax({
                    url:'/wechatSet/updateState',
                    data:data,
                    type:'POST',
                    dataType: 'json',
                    success:function(res){
                        layer.alert(res.message);
                        if(res.code == 0){
                            $(object).attr('data-state',newState);
                            $(object).text(name);
                            $('.stateType_'+id).text(alias);
                        }

                    }
                });
                layer.close(index);
                return true;
            }, function(index){
                layer.close(index);
            });
        }
    </script>
@endsection